package at.connectme.importer;

import kiwi.core.api.importer.Importer;
import kiwi.core.api.importer.KiWiImportException;
import kiwi.core.api.task.Task;
import kiwi.core.api.task.TaskManagerService;
import kiwi.core.api.transaction.Transaction;
import kiwi.core.api.transaction.TransactionService;
import kiwi.core.model.rdf.KiWiResource;
import kiwi.core.services.importer.rdf.RDFImporterImpl;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.*;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * User: Thomas Kurz
 * Date: 26.01.12
 * Time: 13:02
 */
@ApplicationScoped
public class N3ImporterExtended implements Importer {

	@Inject
	RDFImporterImpl rdfImporter;

	@Inject
    private TransactionService transactionService;

    @Inject
    private TaskManagerService taskManagerService;

	private static String[] mime_types = {"text/n3+"};

	@Override
	public String getName() {
		return "N3 Importer Extended";
	}

	@Override
	public String getDescription() {
		return "An importer for huge n3 files";
	}

	@Override
	public Set<String> getAcceptTypes() {
		return new HashSet<String>(Arrays.asList(mime_types));
	}

	private static final int interval = 1000;

	@Override
	public int importData(URL url, String format, KiWiResource user) throws KiWiImportException {
		Transaction transaction=null;
		int triple = 0;
		try {
			StringBuffer b = new StringBuffer();
			// import from local directory
			BufferedReader r = new BufferedReader( new FileReader(new File(url.getPath())));
			String s = "";
			int count = 0;
			Task task = taskManagerService.getTask();
            task.updateStatus("Importing SNXML from directory " + url.getPath());

            transaction = transactionService.getTransaction();
            transactionService.begin(transaction);

			StringBuffer buffer = new StringBuffer();
			while((s=r.readLine())!=null) {
				if(count!=interval) {
					buffer.append(s);
					triple++;
					count++;
				} else {
					rdfImporter.importData(new StringReader(buffer.toString()),"text/n3",user);
					count=0;
					transactionService.commit(transaction);
					transaction = transactionService.getTransaction();
            		transactionService.begin(transaction);
					task.updateStatus(triple+" triples imported");
				}
			}
			task.updateStatus(triple+" overall triples imported, task finished");
			task.end();
			transactionService.commit(transaction);

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (transaction!=null && !transaction.isCommitted()) {
        	transactionService.abort(transaction);
        	transactionService.rollback(transaction);
        }
		return 0;
	}

	@Override
	public int importData(InputStream is, String format, KiWiResource user) throws KiWiImportException {
		//TODO
		return 0;  //To change body of implemented methods use File | Settings | File Templates.
	}

	@Override
	public int importData(Reader reader, String format, KiWiResource user) throws KiWiImportException {
		//TODO
		return 0;  //To change body of implemented methods use File | Settings | File Templates.
	}
}
